import $store from '@/store/index.js';
import Vue from 'vue';



//promise调用方式
export default {

  // 全局配置
  common: {

    baseUrl: "https://www.baidu.com",
    header: {
      'Content-Type': 'application/json;charset=UTF-8'
      // 'content-type': 'application/texts'

    },
    data: {},
    method: 'GET'
  },




  // 请求 返回promise
  request(options = {}) {
    // 组织参数
    options.url = this.common.baseUrl + options.url;
    // console.log(this.common.domainIndex)
    // options.index = this.common.domainIndex = options.index || 0
    options.header = options.header || this.common.header;
    options.data = options.data || this.common.data;
    options.method = options.method || this.common.method;
    options.dataType = options.dataType || this.common.dataType;


    // token,需要上传token
    if (options.token) {
      var token = uni.getStorageSync('token');
      options.header.token = token;
      // 二次验证是否登录
      // if (options.checkToken && !options.header.token) {
      // 	uni.showToast({
      // 		title: '请先登录',
      // 		icon: 'none'
      // 	});
      // 	return uni.navigateTo({
      // 		url: '/pages/login/login',
      // 	});
      // }
    }

    // 请求
    return new Promise((res, rej) => {
      // 请求之前... todo
      uni.showLoading({
        title: '加载中...',
        mask: true
      });
      // 请求中...
      uni.request({
        ...options,
        withCredentials: false,
        success: (result) => {
          uni.hideLoading();
          // 状态码为200时 拦截处理
          if (result.statusCode == "200") {
            uni.hideLoading();
            switch (result.data.code) {
              case 200:
                res(result.data, result);
                break;
              case 500:
                rej("业务异常", result.data);
                uni.showToast({
                  title: result.data.msg,
                  duration: 1500,
                  icon: 'none'
                });
                break;
              case 1000:
                uni.showToast({
                  title: '请先登录',
                  icon: 'none'
                });
                uni.removeStorageSync('userInfo');
                $store.state.user.userInfo = "";
              // setTimeout(() => {
              //     uni.switchTab({
              //         url: "/pages/my/my" 
              //     })
              // }, 300)
              default:
                uni.showToast({
                  title: result.data.msg,
                  duration: 1500,
                  icon: 'none'
                });
                res(result.data);
                break;}

          } else {
            uni.showToast({
              title: '网络异常',
              icon: 'none',
              duration: 1500
            });
            console.log('==== http报错了 ====');
            rej();
          }

        },
        fail: (error) => {
          uni.hideLoading();
          uni.showToast({
            title: error.errMsg || '请求失败',
            icon: 'none'
          });
          return rej();
        }
      });
    });



  },
  // get请求
  get(url, data = {}, options = {}) {
    options.url = url;
    options.data = data;
    options.method = 'GET';
    return this.request(options);
  },
  // post请求
  post(url, data = {}, options = {}) {
    options.url = url;
    options.data = data;
    options.method = 'POST';
    return this.request(options);
  },
  // put请求
  put(url, data = {}, options = {}) {
    options.url = url;
    options.data = data;
    options.method = 'put';
    return this.request(options);
  },
  // delete请求
  del(url, data = {}, options = {}) {
    options.url = url;
    options.data = data;
    options.method = 'DELETE';
    return this.request(options);
  }
};